home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utmisc1 / alcun101.lha / src / mkcard.c < prev    next >
C/C++ Source or Header  |  1995-01-11  |  3KB  |  131 lines

  1. /*
  2.  *  This file is part of x48, an emulator of the HP-48sx Calculator.
  3.  *  Copyright (C) 1994  Eddie C. Dost  (ecd@dressler.de)
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* $Log: mkcard.c,v $
  21.  * Revision 1.1  1995/01/11  18:11:25  ecd
  22.  * Initial revision
  23.  *
  24.  *
  25.  * $Id: mkcard.c,v 1.1 1995/01/11 18:11:25 ecd Exp ecd $
  26.  */
  27.  
  28.  
  29. #include "global.h"
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #ifdef SUNOS
  36. #include <memory.h>
  37. #endif
  38. #include <fcntl.h>
  39.  
  40. unsigned char *mem;
  41.  
  42. int
  43. #ifdef __FunctionProto__
  44. write_mem_file(char *name, unsigned char *mem, int size)
  45. #else
  46. write_mem_file(name, mem, size)
  47. char *name;
  48. unsigned char *mem;
  49. int size;
  50. #endif
  51. {
  52.   FILE *fp;
  53.  
  54.   if (NULL == (fp = fopen(name, "w")))
  55.     {
  56.       fprintf(stderr, "can\'t open %s\n", name);
  57.       return 0;
  58.     }
  59.  
  60.   if (fwrite(mem, 1, (size_t)size, fp) != size)
  61.     {
  62.       fprintf(stderr, "can\'t write %s\n", name);
  63.       fclose(fp);
  64.       return 0;
  65.     }
  66.  
  67.   fclose(fp);
  68.   return 1;
  69. }
  70.  
  71. int
  72. #ifdef __FunctionProto__
  73. main(int argc, char **argv)
  74. #else
  75. main(argc, argv)
  76. int argc;
  77. char **argv;
  78. #endif
  79. {
  80.   long size;
  81.   char *name;
  82.   char *asize;
  83.   unsigned char *core;
  84.  
  85.   if (argc < 2)
  86.     {
  87.       fprintf(stderr, "usage: %s [32K | 128K | 1M | 2M | 4M] file-name\n",
  88.               argv[0]);
  89.       exit (1);
  90.     }
  91.  
  92.   name = argv[2];
  93.   asize = argv[1];
  94.   if (!strcmp(asize, "32K"))
  95.     size = 0x8000;
  96.   else if (!strcmp(asize, "128K"))
  97.     size = 0x20000;
  98.   else if (!strcmp(asize, "256K"))
  99.     size = 0x40000;
  100.   else if (!strcmp(asize, "512K"))
  101.     size = 0x80000;
  102.   else if (!strcmp(asize, "1M"))
  103.     size = 0x100000;
  104.   else if (!strcmp(asize, "2M"))
  105.     size = 0x200000;
  106.   else if (!strcmp(asize, "4M"))
  107.     size = 0x400000;
  108.   else
  109.     {
  110.       fprintf(stderr,
  111.               "%s: size must be one of 32K, 128K, 256K, 512K, 1M, 2M, or 4M\n",
  112.               argv[0]);
  113.       exit (1);
  114.     }
  115.  
  116.   if ((core = (unsigned char *)malloc(size)) == NULL) {
  117.     fprintf(stderr, "%s: can\'t malloc %ld bytes\n", argv[0], size);
  118.     exit (1);
  119.   }
  120.   memset(core, 0, size);
  121.  
  122.   if (!write_mem_file(name, core, size))
  123.     {
  124.       fprintf(stderr, "%s: can\'t write to %s\n", argv[0], name);
  125.       exit (1);
  126.     }
  127.  
  128.   exit (0);
  129. }
  130.  
  131.